package com.project.movie.controller.theater;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieCinema;
import com.project.movie.domain.MovieTheater;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieCinemaService;
import com.project.movie.service.MovieTheaterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 影厅类型controller
 * @date 2025/02/06 11:23
 */
@Controller
@ResponseBody
@RequestMapping("theater")
public class MovieTheaterController {

    @Autowired
    private MovieTheaterService movieTheaterService;
    @Autowired
    private MovieCinemaService movieCinemaService;

    /** 分页获取影厅类型 */
    @PostMapping("getMovieTheaterPage")
    public Result getMovieTheaterPage(@RequestBody MovieTheater movieTheater) {
        Page<MovieTheater> page = new Page<>(movieTheater.getPageNumber(),movieTheater.getPageSize());
        QueryWrapper<MovieTheater> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieTheater.getName()),MovieTheater::getName,movieTheater.getName());
        Page<MovieTheater> movieTheaterPage = movieTheaterService.page(page, queryWrapper);
        return Result.success(movieTheaterPage);
    }

    @GetMapping("getMovieTheaterList")
    public Result getMovieTheaterList() {
        List<MovieTheater> theaterList = movieTheaterService.list();
        return Result.success(theaterList);
    }

    /** 根据id获取影厅类型 */
    @GetMapping("getMovieTheaterById")
    public Result getMovieTheaterById(@RequestParam("id")String id) {
        MovieTheater movieTheater = movieTheaterService.getById(id);
        return Result.success(movieTheater);
    }

    /** 保存影厅类型 */
    @PostMapping("saveMovieTheater")
    public Result saveMovieTheater(@RequestBody MovieTheater movieTheater) {
        boolean save = movieTheaterService.save(movieTheater);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑影厅类型 */
    @PostMapping("editMovieTheater")
    public Result editMovieTheater(@RequestBody MovieTheater movieTheater) {
        boolean save = movieTheaterService.updateById(movieTheater);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除影厅类型 */
    @GetMapping("removeMovieTheater")
    public Result removeMovieTheater(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieTheater movieTheater = movieTheaterService.getById(id);
                QueryWrapper<MovieCinema> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().like(MovieCinema::getTheater,movieTheater.getName());
                int count = movieCinemaService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("影厅类型下存在影院无法删除");
                }
                movieTheaterService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("影厅类型id不能为空！");
        }
    }

}
